using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBSourceToolsLib.Composite;

namespace DBSourceTools
{
    public partial class SearchResultsForm : MainPanelForm, DBSourceToolsLib.Interfaces.IDBExplorerSubject
    {
        List<DBSourceToolsLib.Interfaces.IDBExplorerObserver> m_ListObservers
            = new List<DBSourceToolsLib.Interfaces.IDBExplorerObserver>();

        public string SearchText;
        public bool MatchTheWholeWord;
        public bool SearchInComments;
        public bool SearchInStrings;

        private DataSet m_DSResults;
        public SearchResultsForm(string strDBName, string strObjType, string strName)
            : base(strDBName, strObjType, strName)
        {
            InitializeComponent();
            m_DSResults = getDataSet();
        }


        private DataSet getDataSet()
        {
            DataSet oDS = new DataSet();
            oDS.Tables.Add("SearchResults");
            oDS.Tables[0].Columns.Add("Database");           
            oDS.Tables[0].Columns.Add("FileName");
            oDS.Tables[0].Columns.Add("DatabaseName");
            oDS.Tables[0].Columns.Add("TypeName");
            oDS.Tables[0].Columns.Add("ObjectName");
            oDS.Tables[0].Columns.Add("LineNo");
            oDS.Tables[0].Columns.Add("Line");

            return oDS;
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {

            this.Text = "Search results for : " + this.SearchText;
            this.TabText = "Search results for: " + this.SearchText;
            this.m_dbSourceListView.DataSet = this.m_DSResults;
            this.m_dbSourceListView.AddColumn("Database", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbSourceListView.AddColumn("TypeName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbSourceListView.AddColumn("FileName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbSourceListView.AddColumn("LineNo", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbSourceListView.AddColumn("Line", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);

            if (!String.IsNullOrEmpty(this.SearchText))
                {

                DoSearch();
            }
            
        }

        public void DoSearch()
        {
            this.Text = "Search results for : " + this.SearchText;
            this.TabText = "Search results for : " + this.SearchText + " ( ";
            if ( MatchTheWholeWord ) this.TabText += "whole words only, ";
            this.TabText += "comments " + ( SearchInComments ? "included" : "excluded" );
            this.TabText += ", string literals " + ( SearchInStrings ? "included" : "excluded" ) + " )";

            // reuse the search engine, all criteria are the same for all files
            DBSourceToolsLib.SysObjects.SearchEngine searchEngine =
                    new DBSourceToolsLib.SysObjects.SearchEngine( this.SearchText, 
                    MatchTheWholeWord, SearchInComments, SearchInStrings );

            this.m_DSResults.Tables[0].Rows.Clear();
            foreach (DBSourceToolsLib.SourceDatabase oSource in DBSourceToolsLib.UserSettings.Instance().DatabaseProject.SourceDatabases)
            {
                foreach (DBSourceToolsLib.SysObjects.SysObject oSysObject 
                    in oSource.SysObjects.SysObjectsList.Values)
                {
                    SearchSysObject(searchEngine, oSource, oSysObject);
                    
                }
                // search patches 
                foreach( DBSourceToolsLib.TargetDatabase oTarget in oSource.TargetDatabases) {
                    foreach( DBSourceToolsLib.SysObjects.SOPatch oSOPatch in oTarget.PatchList) {
                        SearchSysObject(searchEngine, oTarget, oSOPatch);
                    }
                }

            }
            this.m_dbSourceListView.Bind();
        }

        private void SearchSysObject(DBSourceToolsLib.SysObjects.SearchEngine searchEngine, 
            DBSourceToolsLib.DatabaseEx oSource, DBSourceToolsLib.SysObjects.SysObject oSysObject)
        {
            List<DBSourceToolsLib.SysObjects.SearchResultLine> oList = new List<DBSourceToolsLib.SysObjects.SearchResultLine>();

            if (oSysObject.SearchText(searchEngine, ref oList))
            {
                string[] strRowVal = new string[7];
                strRowVal[0] = oSource.NickName;
                strRowVal[1] = oSysObject.Name;
                strRowVal[2] = oSysObject.DBName;
                strRowVal[3] = oSysObject.Type.ToString() + "s";
                strRowVal[4] = oSysObject.FileName;
                
                foreach (DBSourceToolsLib.SysObjects.SearchResultLine oLine in oList)
                {
                    strRowVal[5] = oLine.LineNo.ToString();
                    strRowVal[6] = oLine.SourceLine;
                    this.m_DSResults.Tables[0].Rows.Add(strRowVal);
                }

            }
        }

        private void m_dbSourceListView_DoubleClick(object sender, EventArgs e)
        {
            DataRow oRow = this.m_DSResults.Tables[0].Rows[ this.m_dbSourceListView.CurrentRow];
            // TODO : COMPLETE 1.2.0.9 Search result form double click.

            WindowList.Instance().ShowWindow(new TreeNodeTag(
                oRow["DatabaseName"].ToString(),
                "File", oRow["ObjectName"].ToString(), null), oRow["LineNo"].ToString());
                
        }



        #region IDBExplorerSubject Members

        public void RegisterInterestInDbExplorer(DBSourceToolsLib.Interfaces.IDBExplorerObserver observer)
        {
            this.m_ListObservers.Add(observer);
        }

        #endregion
    }
}